window.wxUtils = {
	ctx:'/admin',
	dicData:{},
	/**
	 * 加载数据字典
	 * 使用例子
	 * wxUtils.loadDicData({
    		dicName:'serviceType',
    		urlFun:function(){
    			return JSON.stringify({"method":"bigservice"});
    		},
    		showFun:function(data){
    			var item = wxUtils.getDicItem('serviceType','SERVICE_TYPE_CODE',typeObj.html());
    			typeObj.html(item['SERVICE_TYPE_NAME']);
    		}
    	});
	 * @param opts
	 */
	loadDicData:function(opts){
		opts = $.extend({
            showFun:function(){}
        },opts);
		var reqParam = opts.urlFun.call(null);
		$.getJSON(wxUtils.ctx+'/weixin/ajax.do',{param:reqParam},function(data){
			wxUtils.dicData[opts.dicName]=data.datalist;
			opts.showFun.call(null,data);
		});
	},
	/**
	 * 读取数据字典数据项
	 * @param dicName 数据字典名称
	 * @param itemCodeName 匹配数据项名称
	 * @param itemCodeValue 匹配数据项值
	 * @returns
	 */
	getDicItem:function(dicName,itemCodeName,itemCodeValue){
		var item = null
		if(wxUtils.dicData[dicName]){
			var dics = wxUtils.dicData[dicName];
			$.each(dics,function(index,obj){
				if(obj[itemCodeName]==itemCodeValue){
					item =  obj;
				}
			});
			return item;
		}
	},
	/**
	 * 加载列表数据，待分页功能
	 * 使用例子
	 * wxUtils.loadDataPage({
	    		urlFun:function(pageNo){
	    			return JSON.stringify({"service_new_type1": "","service_new_type2": "","pageNo": pageNo,"method":"findService"});
	    		},
	    		showFun:function(data){
	    			console.info(data.datalist);
	    		}
	    });	 
	 * @param opts
	 */
	loadDataPage:function(opts){
		opts = $.extend({
            showFun:function(){}
        },opts);
		$("#moreBtn").unbind("click");
		$("#moreBtn").bind("click",function(){
			var btn = $(this);
			var pageNo = parseInt(btn.attr("pageNo"));
			var pageCount = parseInt(btn.attr("pageCount"));
			if(pageNo<pageCount){
				wxUtils.showToast();
				btn.parent().hide();
				pageNo++;
	    		var reqParam = opts.urlFun.call(null,pageNo);
				$.getJSON(wxUtils.ctx+'/weixin/ajax.do',{param:reqParam},function(data){
					opts.showFun.call(null,data);
					btn.attr("pageNo",pageNo);
					btn.attr("pageCount",data.pageCount);
					if(data.pageNo==data.pageCount){
						btn.parent().hide();
					}else{
						btn.parent().show();
					}
					wxUtils.closeToast();
				});
			}
		});
		$("#moreBtn").click();
	},
	/**
	 * 加载数据
	 * 使用例子
	 * wxUtils.loadData({
	    		urlFun:function(){
	    			return JSON.stringify({"id":"${param.id}","method":"service_info"});
	    		},
	    		showFun:function(data){
	    			 var bean = data.datalist[0];
	    		}
	    });	 
	 * @param opts
	 */
	loadData:function(opts){
		opts = $.extend({
			isdecode:false,
            showFun:function(){}
        },opts);
		var reqParam = opts.urlFun.call(null);
		$.getJSON(wxUtils.ctx+'/weixin/ajax.do',{isdecode:opts.isdecode,param:reqParam},function(data){
			opts.showFun.call(null,data);
		});
	},
	/**
	 * ajax请求（要求服务端返回JSON格式数据）
	 * wxUtils.ajax({url:'/weixin/loginUser.do',callFun:function(data){
			if(!data){
				alert("未登录，请先登录！")
			}
		}});
	 * @param opts
	 */
	ajax:function(opts){
		opts = $.extend({
			param:{},
            callFun:function(){}
        },opts);
		$.getJSON(wxUtils.ctx+opts.url,opts.param,function(data){
			opts.callFun.call(null,data);
		});
	},
	alert:function(text,callFun){
		callFun=callFun?callFun:function(){};
		var d = $('<div class="js_dialog" id="alertMsg" style="display: none;">');
		var d_1 = $('<div class="weui-mask"/>').appendTo(d);
		var d_2 = $('<div class="weui-dialog">').appendTo(d);
		var d_2_1 = $('<div class="weui-dialog__bd"/>').appendTo(d_2).text(text);
		var d_2_2 = $('<div class="weui-dialog__ft"/>').appendTo(d_2);
		var d_2_2_1 = $('<a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary"/>').appendTo(d_2_2).text('知道了').click(function(){
			d.fadeIn(100).remove();
			callFun.call(null);
		});
		d.appendTo($("body"));
		d.fadeIn(200);
	},
	confirm:function(text,callFun){
		callFun=callFun?callFun:function(){};
		var d = $('<div class="js_dialog" id="confirmMsg" style="display: none;">');
		var d_1 = $('<div class="weui-mask"/>').appendTo(d);
		var d_2 = $('<div class="weui-dialog">').appendTo(d);
		var d_2_1 = $('<div class="weui-dialog__bd"/>').appendTo(d_2).text(text);
		var d_2_2 = $('<div class="weui-dialog__ft"/>').appendTo(d_2);
		var d_2_2_1 = $('<a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_default"/>').appendTo(d_2_2).text('取消').click(function(){
			d.fadeIn(100).remove();
		});
		var d_2_2_2 = $('<a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary"/>').appendTo(d_2_2).text('确定').click(function(){
			d.fadeIn(100).remove();
			callFun.call(null);
		});
		d.appendTo($("body"));
		d.fadeIn(200);
	},
	/**
	 * 成功提示框
	 * @param text
	 */
	msg:function(text,type,callFun){
		callFun=callFun?callFun:function(){};
		type=type?type:1;
		var d = $('<div id="msgToast" style="display:none"/>');
		var d1 = $('<div class="weui-mask_transparent"/>').appendTo(d);
		var d2 = $('<div class="weui-toast"/>').appendTo(d);
		var i = $('<i class="'+(type==2?'weui-icon-warn weui-icon_msg_1':'weui-icon-success-no-circle')+' weui-icon_toast"/>').appendTo(d2);
		var p = $('<p class="weui-toast__content"/>').appendTo(d2).text(text);
		d.appendTo($("body"));
		d.fadeIn(100);
		if(type==2){
			$("#msgToast").bind('click',function(){
				wxUtils.removeMsg();
				callFun.call(null);
			});
		}else{
			setTimeout(function(){
				wxUtils.removeMsg()
				callFun.call(null);
			},2000);
		}
	},
	/**
	 * 失败提示框
	 * @param text
	 */
	error:function(text){
		var d = $('<div id="toast" style="display:none"/>');
		var d1 = $('<div class="weui-mask_transparent"/>').appendTo(d);
		var d2 = $('<div class="weui-toast"/>').appendTo(d);
		var i = $('<i class="weui-loading weui-icon_toast"/>').appendTo(d2);
		var p = $('<p class="weui-toast__content"/>').appendTo(d2).text(text);
		d.appendTo($("body"));
		d.fadeIn(100);
		setTimeout('wxUtils.removeMsg()',2000);
	},
	removeMsg:function(){
		$("#msgToast").fadeOut(200).remove();
	},
	menuClick:function(menu){
		if(menu=='1'){
			window.location=wxUtils.ctx+"/weixin/govservice.do";
		}else if(menu=='2'){
			window.location=wxUtils.ctx+"/weixin/service.do";
		}else if(menu=='3'){
			window.location=wxUtils.ctx+"/weixin/notice.do";
		}else if(menu=='4'){
			window.location=wxUtils.ctx+"/weixin/login.do";
		}
	},
	linkClick:function(url){
		window.location=wxUtils.ctx+url;
	},
	loadScrollTop:0,
	load:function(url){
		wxUtils.loadScrollTop=$(window).scrollTop();
		$(".list_panel").hide();
		$(".detail_panel").show().load(wxUtils.ctx+url);
	},
	returnList:function(){
		$(".detail_panel").empty().hide();
		$(".list_panel").show();
		$(window).scrollTop(wxUtils.loadScrollTop);  
	},
	serviceType:[
	 {code:"01",name:"柜台式服务"},
	 {code:"02",name:"电话服务"},
	 {code:"03",name:"上门服务"},
	 {code:"04",name:"刊物、信函服务"},
	 {code:"05",name:"网络服务"},
	 {code:"06",name:"合同服务"},
	 {code:"07",name:"其他服务"}],
    getServiceWay:function(code){
	     var serviceName = "";
	     $.each(wxUtils.serviceType,function(index,o){
	         if(o.code==code){
	             serviceName = o.name;   
	         }
	     });
	     return serviceName;
	 },
	 initSearchBar:function(){
		var $searchBar = $('#searchBar'),$searchResult = $('#searchResult'),
        $searchText = $('#searchText'),$searchInput = $('#searchInput'),
        $searchClear = $('#searchClear'),$searchCancel = $('#searchCancel');
	
	    $searchText.on('click', function(){
	        $searchBar.addClass('weui-search-bar_focusing');
	        $searchInput.focus();
	    });
	    
	    $searchInput.on('blur', function () {
	            if(!this.value.length){
	            	$searchResult.hide();
	    	        $searchInput.val('');
	    	        $searchBar.removeClass('weui-search-bar_focusing');
	    	        $searchText.show();
	            }
	        }).on('input', function(){
	            if(this.value.length) {
	                $searchResult.show();
	            } else {
	                $searchResult.hide();
	            }
	    });
	    
	    $searchClear.on('click', function(){
	    	$searchResult.hide();
	        $searchInput.val('');
	        $searchInput.focus();
	    });
	    
	    $searchCancel.on('click', function(){
	    	$searchResult.hide();
	        $searchInput.val('');
	        $searchBar.removeClass('weui-search-bar_focusing');
	        $searchText.show();
	        $searchInput.blur();
	    });
	},
	
	/**
	 * 数据加载中等待提示框
	 * @param content
	 */
	showToast: function(content){
		var body = $("body");
		if($("#loadingToast").length>0){
			$("#loadingToast").show();
		}else{
			var toastHtml = '<div id="loadingToast">'+
							   	'<div class="weui-mask_transparent"></div>'+
							   	'<div class="weui-toast">'+
							   		'<i class="weui-loading weui-icon_toast"></i>'+
							   		'<p class="weui-toast__content">'+(content?content:'数据加载中')+'</p>'+
							   	'</div>'+
						   '</div>';
			body.append(toastHtml);
			
		}
	},
	
	/**
	 * 销毁数据加载中等待提示框
	 * @param content
	 */
	closeToast : function(){
		$("#loadingToast").remove();
	},
	
	getItemHtml : function(type,cfg){
		cfg = $.extend({
			url:'',
			id:'',
			title:'',
			desc:'',
			badge1:'',
			badge2:'',
			badge3:'',
			badge4:'',
			foot:''
		},cfg);
		var html = '';
		if(type==0){
			html = '<a href="javascript:{0}" class="weui-media-box weui-media-box_appmsg" id="{1}">'+
					'<div class="weui-media-box__bd">'+
						'<h4 class="weui-media-box__title">{2}</h4>'+
						'<p class="weui-media-box__desc">{3}</p>'+
					'</div>'+
				  '</a>';
			html=html.format(cfg.url,cfg.id,cfg.title,cfg.desc)
		}else{
			html = '<a href="javascript:{0}" class="weui-media-box weui-media-box_appmsg" id="{1}">'+
  					'<div class="weui-media-box__bd">'+
  						'<h4 class="weui-media-box__title">{2}</h4>'+
  						'<p class="weui-media-box__desc">{3}</p>'+
  						'<ul class="weui-media-box__info_e">'+
  							'<li class="weui-media-box__info__meta">{4}</li>'+
	      					'<li class="weui-media-box__info__meta">{5}</li>'+
	      					'<li class="weui-media-box__info__meta">{6}</li>'+
	      					'<li class="weui-media-box__info__meta">{7}</li>'+
  						'</ul>'+
  						'<ul class="weui-media-box__info_e">{8}</ul>'+
  					'</div>'+
  				  '</a>';
  			html=html.format(cfg.url,cfg.id,cfg.title,cfg.desc,cfg.badge1,cfg.badge2,cfg.badge3,cfg.badge4,cfg.foot)
		}
  		return html;
	},
	
	/**
	 * 初始化滚动条位置
	 */
	iniScrollBar : function(){
		var body,top,bottom,center,w=0,tw=0,bw=0,cw=0;
		body = $(window);
		top = $("*[position='top']");
		bottom = $("*[position='bottom']");
		top = $("*[position='top']");
		center = $("*[position='center']");
		if(center.length>0){
			w=body.height();
			if(top.length>0){
				tw = top.height();
			}
			if(bottom.length>0){
				bw = bottom.height();
			}
			cw = w-tw-bw;
			center.height(cw);
			center.css("overflow","auto");
			center.css("overflow-x","hidden");
			$("body").css("overflow","hidden");
			center.parent().css("overflow","hidden");
		}
	},
	
	getUserInfo : function(callFun){
		callFun=callFun?callFun:function(){};
		$.post(wxUtils.ctx+"/weixin/getLoginUserInfo.do",{},function(data){
			if(data.success){
				callFun.call(null,data.obj);
			}else{
				window.location.href = wxUtils.ctx + "/weixin/error.do?error="+data.msg; 
			}
		},'json');
	},
	//服务信息状态
	getServiceStatus:function(status){
        if(status=='0'){
            return '未受理';
        }else if(status=='1'){
            return '受理中';
        }else if(status=='2'){
            return '已处理';
        }else if(status=='3'){
            return '不受理';
        }
        return '';
    },
    getServiceMenuName:function(menuCode){
        if(menuCode=='tmp_f107'){
            return '我的申请';
        }else if(menuCode=='tmp_f102'){
            return '未受理服务';
        }else if(menuCode=='tmp_f103'){
            return '受理中服务';
        }else if(menuCode=='tmp_f104'){
            return '已受理服务';
        }else if(menuCode=='tmp_f109'){
            return '不受理服务';
        }
        return '';
    },
    defaultIfEmpty:function(value,def){
    	if(!value || value=='null' || value==''){
    		if(def){
    			return def;
    		}
    		return '';
    	}
    	return value;
    },
    isNumber:function(number){
        //var reg = /^[0-9]*$/;;//用来验证数字，包括小数的正则
        if(!isNaN(number)){
            return true;
        }else{
            return false;
        }
    },
    
    strFormat:function(value){
    	return value.replace(new RegExp("%2F","gm"),"/").replace(new RegExp("\\+","gm"),"").replace(new RegExp("%26","gm"),"&").replace(new RegExp("%3B","gm"),";");
    },
    
    titleFormat:function(value){
    	return value.replace(new RegExp("%20","gm"),"");
    },
    contentImg:function(contentstr){
        var content = $("<div/>");
        content.html(contentstr);
        var imgs = content.find("img");
        $.each(imgs,function(i,img){
            var obj = $(img);
            if(parseInt(obj.attr('width'))>300){
                obj.attr('width','100%').removeAttr('height');
            }
        });
        return content.html();
    }
}
